<template>
  <div class="page" v-loading="loading">
    <div style="margin:15px;">
      <el-form :inline="true" :model="info" class="demo-form-inline">
        <el-form-item label="题目名称:">
          <el-input @keyup.enter.native="initPage" v-model="info.name" placeholder="请输入题目关键字搜索"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="initPage"><i class="el-icon-search"></i> 查询</el-button>
          <el-button type="warning" plain @click="close"><i class="el-icon-delete"></i> 清除</el-button>
          <el-button type="primary" plain @click="flag = true"><i class="el-icon-download"></i> 导入Word文档</el-button>
          <el-button type="success" plain @click="downloadFile()"><i class="el-icon-upload2"></i> 下载模板</el-button>
          <el-button type="info" @click="flag = false;" v-if="flag">取消上传</el-button>
          <el-button type="success" @click="uploadFile" v-if="flag">开始上传</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="padding: 30px; text-align: center; " v-if="flag">
        <el-upload class="upload-demo" drag :action="$api + '/barrier/upload_word_file'"
                   :auto-upload="false" ref="upload">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        </el-upload>
    </div>
    <div style="margin-bottom: 15px;">
      <el-table :data="answerList" border>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="唯一编号" prop="id" width="100"></el-table-column>
        <el-table-column label="题目名称" prop="title" width="200"></el-table-column>
        <el-table-column label="正确答案" sortable prop="answer" width="150"></el-table-column>
        <el-table-column label="展示选项" width="450" prop="list"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <div>
              <el-tag size="mini" v-if="scope.row.disabled == 0">正常 </el-tag>
              <el-tag v-else　type="danger" size="mini">已禁用</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <div>
              <el-button type="primary" size="mini" @click="updateItem(scope.row)"><i class="el-icon-edit"></i> 修改题目</el-button>
              <el-button type="danger" size="mini" @click="disable(scope.row)" v-if="scope.row.disabled == 0"><i class="el-icon-delete"></i>
                禁用题目</el-button>
              <el-button type="success" size="mini" @click="disable(scope.row)" v-else><i class="el-icon-circle-plus-outline"></i>
                启用题目</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="display: flex;justify-content: flex-end;align-items: center;">
      <el-pagination :total="total" :page-size="info.limit" @current-change="currentChange" @size-change="handleSizeChange"
        background layout="sizes,prev,pager,next,jumper"></el-pagination>
    </div>
    <el-dialog title="修改题目信息" :visible.sync="show" width="40%" center>
      <el-form ref="form" :model="updateObj" label-width="100px">
        <el-form-item label="题目ID:">
          <el-input v-model="updateObj.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="题目名称:">
          <el-input v-model="updateObj.title" placeholder="请输入题目名称"></el-input>
        </el-form-item>
        <el-form-item label="正确答案:">
          <el-input v-model="updateObj.answer" placeholder="请输入答案"></el-input>
        </el-form-item>
        <el-form-item label="展示列表:">
          <el-input v-model="updateObj.list" placeholder="请输入题目所需要的展示列表" type="textarea"></el-input>
        </el-form-item>
        <el-form-item style="text-align: right">
          <el-button type="info" @click="show = false">取 消</el-button>
          <el-button type="primary" @click="updateSubmit">提 交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  download
} from "@/utils/util";

  export default {
    name: "index",
    data() {
      return {
        info: {
          content: "",
          page: 1,
          limit: 10,
          isAll: "all"
        },
        answerList: [],
        total: 0,
        show: false,
        updateObj: {
          id: 0,
          title: '',
          answer: '',
          list: '',
        },
        loading:false,
        flag:false,
      }
    },
    methods: {
      initPage() {
        this.loading = true
        this.$get("/barrier/problem_list", this.info).then(res => {
          if (res.code == 200) {
            this.answerList = res.data.records;
            this.total = res.data.total;
          }
          this.loading = false
        })
      },
      uploadFile(){
        this.$confirm("上传后不能撤销，是否继续上传？").then(abc => {
          this.$refs.upload.submit();
          this.flag = false;
          this.$message.success('已上传，请等待结果');
          this.initPage();
        })
      },
      currentChange(page) {
        this.info.page = page;
        this.initPage()
      },
      handleSizeChange(limit) {},
      close() {
        this.info = {
          categoryId: '',
          name: "",
          page: 1,
          limit: 10
        }
      },
      updateItem(item) {
        let abc = this.updateObj;
        abc.id = item.id, abc.title = item.title, abc.answer = item.answer, abc.list = item.list;
        this.show = true;
      },
      updateSubmit() {
        this.$post('/barrier/update_problem', this.updateObj).then(res => {
          if (res.code == 200) {
            this.$message.success(res.data);
            this.initPage();
          } else
            this.$message.warning(res.data);
          this.show = false;
        })
      },
      downloadTemplate() {
        this.$get("/barrier/template", {
          type: 0
        }).then(result => {
          this.$message.success("请等待下载完成");
        })
      },
      disable(data) {
        let message = "禁用该题目后该题目不会再出现在已有的关卡中，是否禁用该题目？";
        if (data.disabled == 1) {
          message = "是否解封此题目？"
        }
        this.$confirm(message).then(abc => {
          this.$post("/barrier/disable", {
            id: data.id,
            changeMethod: 0
          }).then(res => {
            if (res.code == 200) {
              this.$message.success(res.data);
              this.initPage();
            } else
              this.$message.warning(res.data);
          })
        })
      },
      downloadFile(){
        download('题目信息', "/static/res/wordTemplate.docx")
      }
    },
    created() {
      this.initPage()
    }
  };
</script>

<style scoped>
  .page {
    border-radius: 8px;
    background-color: #fff;
    height: calc(100vh - 105px);
    overflow-y: auto;
    box-sizing: border-box;
  }
</style>
